package com.zzyl.service.impl;

import com.zzyl.service.UserService;
import com.zzyl.vo.UserAuth;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 *  通过UserDetailServicehImpl实现类，实现UserDetailService接口 -> 获取认证用户
 *  获取用户明细信息服务
 *  自定义UserDetailServiceImpl实现类，实现Spring Security中的UserDetailsService接口
 *  通过@Component声明UserDetailServiceImplSpring为Spring管理组件，通知Spring这个类注册为一个Bean，可以被Spring容器自动装配
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserService userService;

    /**
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //这里我们默认使用账号密码登录,对于多种登录方式如何处理-->字符串分割
        //通过username获取当前登录用户信息到userVo中
        UserVo userVo = userService.findUserVoForLogin(username);
        //UserAuth实现类   实现UserDetails接口  根据userVo创建一个UserAuth对象后返回
        return new UserAuth(userVo);
    }
}
